package com.kk.testserverservice;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.LayoutInflater;


import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.ViewCompat;

import com.kk.service.IKKTestInterface;
import com.kk.testserverservice.databinding.ActivityMainBinding;
import com.kk.testserverservice.model.MainModel;

public class MainActivity extends AppCompatActivity implements IBtnControl {

    private static final String TAG = "_main_";
    protected MainModel mMainModel = new MainModel();
    private ActivityMainBinding mBinding = null;

    private IKKTestInterface mIKKTestInterface;


    private KKServiceConnect mKKServiceConnect = new KKServiceConnect();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = ActivityMainBinding.inflate(LayoutInflater.from(this));
        setContentView(mBinding.getRoot());
        mBinding.setData(mMainModel);
        mMainModel.setIBtnControl(this);
    }


    private volatile boolean mBound = false;

    @Override
    public void startService() {

        Log.i(TAG, "startService: ");


        reConnectService();

    }


    void reConnectService() {
        if (!mBound) {
            Intent intent = new Intent();
            ComponentName componentName = new ComponentName("com.kk.service", "com.kk.service.KKService");
            intent.setComponent(componentName);
            bindService(intent, mKKServiceConnect, Context.BIND_AUTO_CREATE);
        }
    }

    int mCount = 0;

    @Override
    public void addString() {

        reConnectService();
        mCount++;
        Log.i(TAG, "addString: ");
        if (mIKKTestInterface != null) {
            try {
                mIKKTestInterface.addMessage(mCount + "");
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void getStringSize() {

        reConnectService();
        Log.i(TAG, "getStringSize: ");
        if (mIKKTestInterface != null) {
            try {
                int size = mIKKTestInterface.getMessageSize();
                Log.i(TAG, "getStringSize: " + size);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void getString() {

        reConnectService();
        Log.i(TAG, "getString: ");

        if (mIKKTestInterface != null) {
            try {
                String msg = mIKKTestInterface.getMessage();
                Log.i(TAG, "getString: " + msg);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }


    private final class KKServiceConnect implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {

            mBound = true;
            mIKKTestInterface = IKKTestInterface.Stub.asInterface(service);
            Log.i(TAG, "onServiceConnected ComponentName: " + name);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

            Log.i(TAG, "onServiceDisconnected: " + name);
            mBound = false;
            mIKKTestInterface = null;
        }

        @Override
        public void onBindingDied(ComponentName name) {
            Log.i(TAG, "onBindingDied: " + name);
        }

        @Override
        public void onNullBinding(ComponentName name) {
            Log.i(TAG, "onNullBinding: " + name);
        }
    }
}